package hawk.example.jdk.regex;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;

public class HrefMatch {
    public static void main(String[] args) throws MalformedURLException, IOException {
        match("https://www.sina.com.cn");
    }

    private static void match(String urlString) throws MalformedURLException, IOException {

        Objects.requireNonNull(urlString);
        InputStream in = new URL(urlString).openStream();
        var input = new String(in.readAllBytes(), StandardCharsets.UTF_8);

        String patternString = "<a\\s+href\\s*=\\s*(\"[^\"]*\"|[^\\s>]*)\\s*>";
        Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
        pattern.matcher(input)
                .results()
                .map(MatchResult::group)
                .forEach(System.out::println);
    }
}
